#include <vector>
#include <iostream>
#include <fstream>
#include <string>
#include "vertex.h"
#include "mesh.h"

using namespace std;

Mesh::Mesh(){}

Mesh::Mesh(const char *file_name){
	ifstream file;
    file.open(file_name);

	Vertex v;

    if (file.is_open()) {
        file >> N;
		vertices = new Vertex [N];
		indices = new GLubyte [3*(N - 2)];
		for (int i = 0; i < N; i++){
			file >> v;
			vertices[i] = v;
		}
        file.close();
    }
	
	indices[0] = 1;
	indices[1] = 2;
	indices[2] = 0;
	for (int i = 3; i < 3*(N - 2); i = i + 3){
		indices[i] = indices[i - 2];
		indices[i + 1] = indices[i] + 1;
		indices[i + 2] = 0;
	}
}

Mesh::Mesh(Mesh &m): N(m.N){
	vertices = new Vertex [N];
	indices = new GLubyte [3*(N - 2)];
	for (int i = 0; i != N; i++)
		vertices[i] = m.vertices[i];
	for (int i = 0; i != 3*(N - 2); i++)
		indices[i] = m.indices[i];
}

Mesh & Mesh::operator = (Mesh &m){
	if (&m == this)
		return *this;
	N = m.N;
	vertices = new Vertex [N];
	indices = new GLubyte [3*(N - 2)];
	for (int i = 0; i < N; i++)
		vertices[i] = m.vertices[i];
	for (int i = 0; i < 3*(N - 2); i++)
		indices[i] = m.indices[i];
	return *this;
}

Mesh::~Mesh(){
	delete [] vertices;
	delete [] indices;
}

int Mesh::getN(){ return N; }

Vertex* Mesh::getVertices(){ return vertices; }

GLubyte* Mesh::getIndices(){ return indices; }